package com.usian.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.config.RedisClient;
import com.usian.mapper.*;
import com.usian.pojo.*;
import com.usian.service.ItemCatService;
import com.usian.service.ItemParamService;
import com.usian.utils.CatNode;
import com.usian.utils.CatResult;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ItemParamServiceImpl implements ItemParamService {

    @Autowired
    private TbItemParamMapper tbItemParamMapper;
    @Autowired
    private TbItemCatMapper tbItemCatMapper;
    @Autowired
    private TbItemDescMapper tbItemDescMapper;
    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;

    @Autowired
    private RedisClient redisClient;

    @Value("${PROTAL_CATRESULT_KEY}")
    private String PROTAL_CATRESULT_KEY;

    @Value("${ITEM_INFO}")
    private String ITEM_INFO;

    @Value("${BASE}")
    private String BASE;

    @Value("${DESC}")
    private String DESC;

    @Value("${PARAM}")
    private String PARAM;

    @Value("${ITEM_INFO_EXPIRE}")
    private Integer ITEM_INFO_EXPIRE;


    @Override
    public TbItemParam selectItemParamByItemCatId(Long itemCatId) {

        TbItemParamExample example =new TbItemParamExample();
        TbItemParamExample.Criteria criteria = example.createCriteria();
        criteria.andItemCatIdEqualTo(itemCatId);

        List<TbItemParam> itemParamList = tbItemParamMapper.selectByExampleWithBLOBs(example);
        if(itemParamList.size()>0){
            return itemParamList.get(0);
        }
        return null;
    }



    @Override
    public PageResult selectItemParamAll(Integer page, Integer rows) {
        PageHelper.startPage(page,rows);

        TbItemParamExample example = new TbItemParamExample();
        TbItemParamExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause("updated desc");
        List<TbItemParam> list = tbItemParamMapper.selectByExampleWithBLOBs(example);

        PageInfo<TbItemParam> pageInfo = new PageInfo<TbItemParam>(list);

        PageResult pageResult =new PageResult();
        pageResult.setPageIndex(pageInfo.getPageNum());
        pageResult.setTotalPage(pageInfo.getPages());
        pageResult.setResult(pageInfo.getList());

        return pageResult;
    }

    @Override
    public Integer insertItemParam(Integer itemCatId, String paramData) {

        TbItemParamExample  example =new TbItemParamExample();
        TbItemParamExample.Criteria criteria = example.createCriteria();
        criteria.andItemCatIdEqualTo(Long.valueOf(itemCatId));
        List<TbItemParam> paramList = tbItemParamMapper.selectByExample(example);
        if(paramList.size()>0){
            return 0;
        }

        TbItemParam itemParam = new TbItemParam();
        itemParam.setItemCatId(Long.valueOf(itemCatId));
        itemParam.setParamData(paramData);
        itemParam.setCreated(new Date());
        itemParam.setUpdated(new Date());
        Integer num = tbItemParamMapper.insertSelective(itemParam);

        //删除缓存的key
        redisClient.del(PROTAL_CATRESULT_KEY);
        return num;
    }

    @Override
    public Integer deleteItemParamById(Integer id) {
        /*TbItemParamExample  example =new TbItemParamExample();
        TbItemParamExample.Criteria criteria = example.createCriteria();
        criteriaeria.andIdEqualTo(Long.valueOf(id));*/

        Integer num = tbItemParamMapper.deleteByPrimaryKey(Long.valueOf(id));

        //删除缓存的key
        redisClient.del(PROTAL_CATRESULT_KEY);
        return num;
    }

    @Override
    public CatResult selectItemCategoryAll() {

        CatResult catResultRedis =(CatResult)redisClient.get(PROTAL_CATRESULT_KEY);
        if(catResultRedis !=null){
            return catResultRedis;
        }else{
            CatResult catResult =new CatResult();
            catResult.setData(getCatList(0L));
            //放入到redis
            redisClient.set(PROTAL_CATRESULT_KEY,catResult);
            return catResult;
        }
    }

    @Override
    public TbItemDesc selectItemDescByItemId(Long itemId) {

        TbItemDesc itemDescRedis =(TbItemDesc) redisClient.get(ITEM_INFO+":"+DESC.concat(":").concat(itemId.toString()));
        if(itemDescRedis!=null){
            return itemDescRedis;
        }

        TbItemDesc itemDesc = tbItemDescMapper.selectByPrimaryKey(itemId);
        if(itemDesc ==null){
            redisClient.set(ITEM_INFO+":"+DESC.concat(":").concat(itemId.toString()),new TbItemDesc(1L,"查无数据"));
        }else{
            redisClient.set(ITEM_INFO+":"+DESC.concat(":").concat(itemId.toString()),itemDesc);
        }
        redisClient.expire(ITEM_INFO+":"+DESC.concat(":").concat(itemId.toString()),ITEM_INFO_EXPIRE);
        return itemDesc;
    }

    @Override
    public TbItemParamItem selectTbItemParamItemByItemId(Long itemId) {
        TbItemParamItem tbItemParamItemRedis = (TbItemParamItem) redisClient.get(ITEM_INFO+":"+PARAM.concat(":").concat(itemId.toString()));
        if(tbItemParamItemRedis!=null){
            return tbItemParamItemRedis;
        }

        TbItemParamItemExample example =new TbItemParamItemExample();
        TbItemParamItemExample.Criteria criteria = example.createCriteria();
        criteria.andItemIdEqualTo(itemId);
        List<TbItemParamItem> list = tbItemParamItemMapper.selectByExampleWithBLOBs(example);
        TbItemParamItem itemParamItem = list.get(0);

        if(itemParamItem==null){
            redisClient.set(ITEM_INFO+":"+PARAM.concat(":").concat(itemId.toString()),new TbItemParamItem(1L,"查无数据"));
        }else{
            redisClient.set(ITEM_INFO+":"+PARAM.concat(":").concat(itemId.toString()),itemParamItem);
        }
        redisClient.expire(ITEM_INFO+":"+PARAM.concat(":").concat(itemId.toString()),ITEM_INFO_EXPIRE);
        return itemParamItem;
    }

    private List<?> getCatList(Long parentId) {

        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbItemCat> list = tbItemCatMapper.selectByExample(example);

        List catNodeList = new ArrayList();
        int count = 0;
        for (TbItemCat itemCat : list) {

            //判断父节点
            if (itemCat.getIsParent()) {
                CatNode catNode = new CatNode();
                catNode.setName(itemCat.getName());
                catNode.setItem(getCatList(itemCat.getId()));
                catNodeList.add(catNode);

                //首页只显示18条
                count++;
                //count = count +1;
                if (count == 18) {
                    break;
                }
            } else {
                catNodeList.add(itemCat.getName());
            }
        }

        return catNodeList;
    }

}
